#!/bin/bash

# AI Writing Platform Setup Script

echo "🚀 Setting up AI Writing Platform..."

# Check prerequisites
check_command() {
    if ! command -v $1 &> /dev/null; then
        echo "❌ $1 is not installed. Please install $1 first."
        exit 1
    fi
}

echo "📋 Checking prerequisites..."
check_command "node"
check_command "docker"
check_command "docker-compose"
check_command "python3"

# Install pnpm if not installed
if ! command -v pnpm &> /dev/null; then
    echo "📦 Installing pnpm..."
    npm install -g pnpm
fi

# Copy environment file
if [ ! -f .env ]; then
    echo "📝 Creating .env file from template..."
    cp .env.example .env
    echo "⚠️  Please edit .env file with your configuration"
fi

# Install dependencies
echo "📦 Installing dependencies..."
pnpm install

# Setup Python virtual environment for API
echo "🐍 Setting up Python environment..."
cd apps/api
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
cd ../..

# Initialize git hooks
echo "🪝 Setting up git hooks..."
pnpm prepare

# Start services
echo "🐳 Starting Docker services..."
docker-compose up -d postgres mongodb redis

# Wait for databases to be ready
echo "⏳ Waiting for databases to be ready..."
sleep 10

echo "✅ Setup complete!"
echo ""
echo "📖 Next steps:"
echo "1. Edit .env file with your configuration"
echo "2. Run 'pnpm dev' to start development servers"
echo "3. Visit http://localhost:3000 for frontend"
echo "4. Visit http://localhost:8000/docs for API documentation"